package org.argus.management.auth.controller;

import org.argus.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.argus.management.auth.model.LoginBody;
import org.argus.common.web.core.domain.LoginUser;
import org.argus.management.auth.model.RefreshTokenReq;
import org.argus.management.auth.model.RefreshTokenResp;
import org.argus.management.auth.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api("Token管理")
@RestController
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public R<LoginUser> login(@RequestBody LoginBody form) {
        return R.ok(tokenService.login(form.getUsername(), form.getPassword()));
    }

    @ApiOperation("刷新访问Token")
    @PostMapping("/refreshToken")
    public R<RefreshTokenResp> refresh(@RequestBody RefreshTokenReq refreshTokenReq) {
        return R.ok(tokenService.refreshToken(refreshTokenReq));
    }

    @ApiOperation("登出")
    @PostMapping("/logout")
    public R<Boolean> logout() {
        tokenService.logout();
        return R.ok(Boolean.TRUE);
    }

    @PutMapping("/switchDomain/{newDomainId}")
    public R<LoginUser> switchDomain(@PathVariable("newDomainId") Long newDomainId) {
        return R.ok(tokenService.switchDomain(newDomainId));
    }
}
